package com.example.springboottest.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * SpringApplicationRunListener 是springboot程序启动的监听事件
 * EventPublishingRunListener 是springboot默认实现监听事件的类
 * 自定义实现SpringApplicationRunListener
 */
public class CustomSpringApplicationRunListener implements SpringApplicationRunListener {
    public CustomSpringApplicationRunListener(SpringApplication sp, String[] args) {
        System.out.println("CustomSpringApplicationRunListener 构造函数执行...");
    }

    @Override
    public void starting() {
        System.out.println("CustomSpringApplicationRunListener.starting...");
    }

    //这步表示加载环境配置，比如spring.profiles.active
    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
//        environment.getActiveProfiles()
//        environment.getSystemEnvironment()
        System.out.println("CustomSpringApplicationRunListener.environment...");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("CustomSpringApplicationRunListener.contextPrepared...");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("CustomSpringApplicationRunListener.running...");
    }
}
